<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>java基础：string类 | haijd</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  <meta name="keywords" content="Java基础" />
  
  
  
  
  <meta name="description" content="Java基础：String类">
<meta name="keywords" content="Java基础">
<meta property="og:type" content="article">
<meta property="og:title" content="Java基础：String类">
<meta property="og:url" content="http://www.hais2.com/2017/05/21/Java基础：String类/index.html">
<meta property="og:site_name" content="haijd">
<meta property="og:description" content="Java基础：String类">
<meta property="og:locale" content="default">
<meta property="og:image" content="http://ww1.sinaimg.cn/large/c55a7aeely1fmbwbz5s71j20et08cjra.jpg">
<meta property="og:updated_time" content="2017-12-13T12:25:59.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java基础：String类">
<meta name="twitter:description" content="Java基础：String类">
<meta name="twitter:image" content="http://ww1.sinaimg.cn/large/c55a7aeely1fmbwbz5s71j20et08cjra.jpg">
  
    <link rel="alternate" href="/atom.xml" title="haijd" type="application/atom+xml">
  
  <link rel="icon" href="/css/images/favicon.ico">
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link href="https://fonts.googleapis.com/css?family=Open+Sans|Montserrat:700" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,400italic" rel="stylesheet" type="text/css">
  <link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
  <style type="text/css">
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/9749f0/00000000000000000001008f/27/l?subset_id=2&fvd=n5) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/90cf9f/000000000000000000010091/27/l?subset_id=2&fvd=n7) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/8a5494/000000000000000000013365/27/l?subset_id=2&fvd=n4) format("woff2");font-weight:lighter;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/d337d8/000000000000000000010095/27/l?subset_id=2&fvd=i4) format("woff2");font-weight:400;font-style:italic;}</style>
    
  <link rel="stylesheet" id="athemes-headings-fonts-css" href="//fonts.googleapis.com/css?family=Yanone+Kaffeesatz%3A200%2C300%2C400%2C700&amp;ver=4.6.1" type="text/css" media="all">
  <link rel="stylesheet" href="/css/style.css">

  <script src="/js/jquery-3.1.1.min.js"></script>

  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="/css/bootstrap.css" >
  <link rel="stylesheet" href="/css/hiero.css" >
  <link rel="stylesheet" href="/css/glyphs.css" >
  
    <link rel="stylesheet" href="/css/vdonate.css" >
  

</head>

<script>
var themeMenus = {};

  themeMenus["/"] = "Home"; 

  themeMenus["/archives"] = "Archives"; 

  themeMenus["/categories"] = "Categories"; 

  themeMenus["/tags"] = "Tags"; 

  themeMenus["/about"] = "About"; 

</script>


  <body data-spy="scroll" data-target="#toc" data-offset="50">


  <header id="allheader" class="site-header" role="banner">
  <div class="clearfix container">
      <div class="site-branding">

          <h1 class="site-title">
            
              <a href="/" title="haijd" rel="home"> haijd </a>
            
          </h1>

          
            <div class="site-description">Stay Hungry,Stay Foolish</div>
          
            
          <nav id="main-navigation" class="main-navigation" role="navigation">
            <a class="nav-open">Menu</a>
            <a class="nav-close">Close</a>
            <div class="clearfix sf-menu">

              <ul id="main-nav" class="nmenu sf-js-enabled">
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/">Home</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/archives">Archives</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/categories">Categories</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/tags">Tags</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/about">About</a> </li>
                    
              </ul>
            </div>
          </nav>


      </div>
  </div>
</header>




  <div id="container">
    <div id="wrap">
            
      <div id="content" class="outer">
        
          <section id="main" style="float:none;"><article id="post-Java基础：String类" style="width: 66%; float:left;" class="article article-type-post" itemscope itemprop="blogPost" >
  <div id="articleInner" class="clearfix post-1016 post type-post status-publish format-standard has-post-thumbnail hentry category-template-2 category-uncategorized tag-codex tag-edge-case tag-featured-image tag-image tag-template">
    
<div class="article-gallery">
  <div class="article-gallery-photos">
    
      <a class="article-gallery-img fancybox" href="http://ww1.sinaimg.cn/large/c55a7aeely1fmbwbz5s71j20et08cjra.jpg" rel="gallery_cjie4nai3004w3swaxi7mmukc">
        <img src="http://ww1.sinaimg.cn/large/c55a7aeely1fmbwbz5s71j20et08cjra.jpg" itemprop="image">
      </a>
    
  </div>
</div>

    
      <header class="article-header">
        
  
    <h1 class="thumb" class="article-title" itemprop="name">
      Java基础：String类
    </h1>
  

      </header>
    
    <div class="article-meta">
      
	Posted on <a href="/2017/05/21/Java基础：String类/" class="article-date">
	  <time datetime="2017-05-21T14:13:33.000Z" itemprop="datePublished">May 21, 2017</time>
	</a>

      
	<span id="busuanzi_container_page_pv">
	  本文总阅读量<span id="busuanzi_value_page_pv"></span>次
	</span>

    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <!-- TOC -->
<ul>
<li><a href="#string类的概念和不变性">String类的概念和不变性</a></li>
<li><a href="#string类创建方式和比较">String类创建方式和比较</a></li>
<li><a href="#string类构造方法">String类构造方法</a></li>
<li><a href="#string类的其他方法">String类的其他方法</a></li>
</ul>
<!-- /TOC -->
<h3 id="String类的概念和不变性"><a href="#String类的概念和不变性" class="headerlink" title="String类的概念和不变性"></a>String类的概念和不变性</h3><ul>
<li>String类的概念和不变性<ul>
<li>String类<ul>
<li>API中的String类的描述，发现String 类代表字符串</li>
<li>Java 程序中的所有字符串字面值（如 “abc” ）都作为此类的实例实现。</li>
<li>字符串是常量,在创建之后不能更改</li>
<li>其实就是说一旦这个字符串确定了，那么就会在内存区域中就生成了这个字符串。字符串本身不能改变，但str变量中记录的地址值是可以改变的。</li>
<li>源码分析,String类底层采用的是字符数组:    <a id="more"></a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">*   String类特点:</span></span><br><span class="line"><span class="comment">*   一切都是对象,字符串事物 "" 也是对象</span></span><br><span class="line"><span class="comment">*   类是描述事物,String类,描述字符串对象的类</span></span><br><span class="line"><span class="comment">*   所有的 "" 都是String类的对象   </span></span><br><span class="line"><span class="comment">*   字符串是一个常量,一旦创建,不能改变</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StringDemo</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">//引用变量str执行内存变化</span></span><br><span class="line">		<span class="comment">//定义好的字符串对象,不变</span></span><br><span class="line">		String str = <span class="string">"123"</span>;</span><br><span class="line">		System.out.println(str);</span><br><span class="line">		str = <span class="string">"456"</span>;</span><br><span class="line">		System.out.println(str);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="String类创建方式和比较"><a href="#String类创建方式和比较" class="headerlink" title="String类创建方式和比较"></a>String类创建方式和比较</h3><ul>
<li>String类创建方式和比较<ul>
<li>创建对象的数量比较<ul>
<li><code>String s3 = &quot;abc&quot;;</code><ul>
<li>在内存中只有一个对象。这个对象在字符串常量池中<br>        - <code>String s4 = new String(&quot;abc&quot;);</code></li>
<li>在内存中有两个对象。一个new的对象在堆中，一个字符串本身对象，在字符串常量池中</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StringDemo2</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">//字符串定义方式2个, 直接“=” or 使用String类的构造方法</span></span><br><span class="line">		String str1 = <span class="keyword">new</span> String(<span class="string">"abc"</span>);</span><br><span class="line">		String str2 = <span class="string">"abc"</span>;</span><br><span class="line">		System.out.println(str1);</span><br><span class="line">		System.out.println(str2);</span><br><span class="line">		System.out.println(str1==str2);<span class="comment">//引用数据类型,比较对象的地址 false</span></span><br><span class="line">		System.out.println(str1.equals(str2));<span class="comment">//true</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="String类构造方法"><a href="#String类构造方法" class="headerlink" title="String类构造方法"></a>String类构造方法</h3><ul>
<li>String类构造方法<ul>
<li>常见构造方法<ul>
<li><code>public String()</code>:空构造</li>
<li><code>public String(byte[] bytes)</code>:把字节数组转成字符串</li>
<li><code>public String(byte[] bytes,int index,int length)</code>:把字节数组的一部分转成字符串            </li>
<li><code>public String(String original)</code>:把字符串常量值转成字符串</li>
</ul>
</li>
</ul>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StringDemo3</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		function();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/*</span></span><br><span class="line"><span class="comment">	*  定义方法,String类的构造方法</span></span><br><span class="line"><span class="comment">	*  String(byte[] bytes)  传递字节数组</span></span><br><span class="line"><span class="comment">	*  字节数组转成字符串</span></span><br><span class="line"><span class="comment">	*  通过使用平台的默认字符集解码指定的 byte 数组，构造一个新的 String。</span></span><br><span class="line"><span class="comment">	*  平台 : 机器操作系统</span></span><br><span class="line"><span class="comment">	*  默认字符集: 操作系统中的默认编码表, 默认编码表GBK</span></span><br><span class="line"><span class="comment">	*  将字节数组中的每个字节,查询了编码表,得到的结果</span></span><br><span class="line"><span class="comment">	*  字节是负数,汉字的字节编码就是负数, 默认编码表 ,一个汉字采用2个字节表示</span></span><br><span class="line"><span class="comment">	*  </span></span><br><span class="line"><span class="comment">	*  String(byte[] bytes, int offset, int length) 传递字节数组</span></span><br><span class="line"><span class="comment">	*  字节数组的一部分转成字符串</span></span><br><span class="line"><span class="comment">	*  offset 数组的起始的索引</span></span><br><span class="line"><span class="comment">	*  length 个数,转几个   , 不是结束的索引</span></span><br><span class="line"><span class="comment">	*/</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">function</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="keyword">byte</span>[] bytes = &#123;<span class="number">97</span>,<span class="number">98</span>,<span class="number">99</span>,<span class="number">100</span>&#125;;</span><br><span class="line">		<span class="comment">//调用String类的构造方法,传递字节数组</span></span><br><span class="line">		String s = <span class="keyword">new</span> String(bytes);</span><br><span class="line">		System.out.println(s);  <span class="comment">//根据ASCII编码，97，98，99，100转成了字符串"abcd"</span></span><br><span class="line">		<span class="keyword">byte</span>[] bytes1 =&#123;<span class="number">65</span>,<span class="number">66</span>,<span class="number">67</span>,<span class="number">68</span>,<span class="number">69</span>&#125;;</span><br><span class="line">		<span class="comment">//调用String构造方法,传递数组,传递2个int值</span></span><br><span class="line">		String s1 = <span class="keyword">new</span> String(bytes1,<span class="number">1</span>,<span class="number">3</span>);</span><br><span class="line">		System.out.println(s1); <span class="comment">//根据ASCII编码，将66，67，68转成了"BCD"</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>其他构造方法<ul>
<li><code>public String(char[] value)</code>:把字符数组转成字符串</li>
<li><code>public String(char[] value,int index,int count)</code>:把字符数组的一部分转成字符串<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StringDemo</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		function();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/*</span></span><br><span class="line"><span class="comment">	* String(char[] value) 传递字符数组</span></span><br><span class="line"><span class="comment">	* 将字符数组,转成字符串, 字符数组的参数,不查询编码表</span></span><br><span class="line"><span class="comment">	* </span></span><br><span class="line"><span class="comment">	* String(char[] value, int offset, int count) 传递字符数组</span></span><br><span class="line"><span class="comment">	* 将字符数组的一部分转成字符串</span></span><br><span class="line"><span class="comment">	* offset  数组开始索引</span></span><br><span class="line"><span class="comment">	* count   个数</span></span><br><span class="line"><span class="comment">	*/</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">function</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="keyword">char</span>[] ch = &#123;<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>,<span class="string">'d'</span>,<span class="string">'e'</span>,<span class="string">'f'</span>&#125;;</span><br><span class="line">		<span class="comment">//调用String构造方法,传递字符数组</span></span><br><span class="line">		String s = <span class="keyword">new</span> String(ch);</span><br><span class="line">		System.out.println(s);   <span class="comment">//直接将字符“拼”起来，输出"abcdef"</span></span><br><span class="line">				</span><br><span class="line">		String s1 = <span class="keyword">new</span> String(ch,<span class="number">1</span>,<span class="number">4</span>);</span><br><span class="line">		System.out.println(s1); <span class="comment">//输出"bcde"</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
<h3 id="String类的其他方法"><a href="#String类的其他方法" class="headerlink" title="String类的其他方法"></a>String类的其他方法</h3><ul>
<li>String类的其他方法<ul>
<li>方法介绍<ul>
<li><code>int length()</code>: 返回字符串的长度</li>
<li><code>String substring(int beginIndex,int endIndex)</code>: 获取字符串的一部分</li>
<li><code>String substring(int beginIndex)</code>: 获取字符串的一部分</li>
<li><code>boolean startsWith(String prefix)</code>: 判断一个字符串是不是另一个字符串的前缀,开头</li>
<li><code>boolean endsWith(String prefix)</code>: 判断一个字符串是不是另一个字符串的后缀,结尾</li>
<li><code>boolean contains (String s)</code>: 判断一个字符串中,是否包含另一个字符串</li>
<li><code>int indexOf(char ch)</code>:  查找一个字符,在字符串中第一次出现的索引,被查找的字符不存在,返回-1</li>
<li><code>byte[] getBytes()</code>: 将字符串转成字节数组,此功能和String构造方法相反,byte数组相关的功能,查询编码表</li>
<li><code>char[] toCharArray()</code>: 将字符串转成字符数组,功能和构造方法相反</li>
<li><code>boolean equals(Object obj)</code>: 方法传递字符串,判断字符串中的字符是否完全相同,如果完全相同返回true</li>
<li><code>boolean equalsIgnoreCase(String s)</code>: 传递字符串,判断字符串中的字符是否相同,忽略大小写            </li>
</ul>
</li>
</ul>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">*  boolean equals(Object obj)</span></span><br><span class="line"><span class="comment">*  方法传递字符串,判断字符串中的字符是否完全相同,如果完全相同返回true</span></span><br><span class="line"><span class="comment">*  </span></span><br><span class="line"><span class="comment">*  boolean equalsIgnoreCase(String s)</span></span><br><span class="line"><span class="comment">*  传递字符串,判断字符串中的字符是否相同,忽略大小写</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">StringFunction</span><span class="params">()</span></span>&#123;</span><br><span class="line">	String str1 = <span class="string">"ABC"</span>;</span><br><span class="line">	String str2 = <span class="string">"abc"</span>;</span><br><span class="line">	<span class="comment">//分别调用equals和equalsIgnoreCase</span></span><br><span class="line">	<span class="keyword">boolean</span> b1 = str1.equals(str2);</span><br><span class="line">	<span class="keyword">boolean</span> b2 = str1.equalsIgnoreCase(str2);</span><br><span class="line">	System.out.println(b1);   <span class="comment">//false</span></span><br><span class="line">	System.out.println(b2);   <span class="comment">//true</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">* char[] toCharArray() 将字符串转成字符数组</span></span><br><span class="line"><span class="comment">* 功能和构造方法相反</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">StringFunction</span><span class="params">()</span></span>&#123;</span><br><span class="line">	String str = <span class="string">"haijd"</span>;</span><br><span class="line">	<span class="comment">//调用String类的方法toCharArray()</span></span><br><span class="line">	<span class="keyword">char</span>[] ch = str.toCharArray();</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span> ; i &lt; ch.length ; i++)&#123;</span><br><span class="line">		System.out.println(ch[i]);   <span class="comment">//输出单个字符</span></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">*  byte[] getBytes() 将字符串转成字节数组</span></span><br><span class="line"><span class="comment">*  此功能和String构造方法相反</span></span><br><span class="line"><span class="comment">*  byte数组相关的功能,查询编码表</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">StringFunction</span><span class="params">()</span></span>&#123;</span><br><span class="line">	String str = <span class="string">"123456"</span>;</span><br><span class="line">	<span class="comment">//调用String类方法getBytes字符串转成字节数组</span></span><br><span class="line">	<span class="keyword">byte</span>[] bytes = str.getBytes();</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span> ; i &lt; bytes.length ; i++)&#123;</span><br><span class="line">		System.out.println(bytes[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//输出：49，50，51，52，53，54</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">*  int indexOf(char ch)</span></span><br><span class="line"><span class="comment">*  查找一个字符,在字符串中第一次出现的索引</span></span><br><span class="line"><span class="comment">*  被查找的字符不存在,返回-1</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">StringFunction</span><span class="params">()</span></span>&#123;</span><br><span class="line">	String str = <span class="string">"haijd"</span>;</span><br><span class="line">	<span class="comment">//调用String类的方法indexOf</span></span><br><span class="line">	<span class="keyword">int</span> index = str.indexOf(<span class="string">'z'</span>);</span><br><span class="line">	System.out.println(index);   <span class="comment">//输出:-1</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">*  boolean contains (String s)</span></span><br><span class="line"><span class="comment">*  判断一个字符串中,是否包含另一个字符串</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">StringFunction</span><span class="params">()</span></span>&#123;</span><br><span class="line">	String str = <span class="string">"haijd"</span>;</span><br><span class="line">	<span class="comment">//调用String类的方法contains</span></span><br><span class="line">	<span class="keyword">boolean</span> b =str.contains(<span class="string">"ai"</span>);</span><br><span class="line">	System.out.println(b); <span class="comment">//输出true</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">* boolean endsWith(String prefix)</span></span><br><span class="line"><span class="comment">* 判断一个字符串是不是另一个字符串的后缀,结尾</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">StringFunction</span><span class="params">()</span></span>&#123;</span><br><span class="line">	String str = <span class="string">"haijd"</span>;</span><br><span class="line">	<span class="comment">//调用String类方法endsWith</span></span><br><span class="line">	<span class="keyword">boolean</span> b = str.endsWith(<span class="string">"jd"</span>);</span><br><span class="line">	System.out.println(b);  <span class="comment">//输出：true</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">* boolean startsWith(String prefix)  </span></span><br><span class="line"><span class="comment">* 判断一个字符串是不是另一个字符串的前缀,开头</span></span><br><span class="line"><span class="comment">* howareyou</span></span><br><span class="line"><span class="comment">* hOw</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">StringFunction</span><span class="params">()</span></span>&#123;</span><br><span class="line">	String str = <span class="string">"haijd"</span>;</span><br><span class="line">	<span class="comment">//调用String类的方法startsWith</span></span><br><span class="line">	<span class="keyword">boolean</span> b = str.startsWith(<span class="string">"Hai"</span>);</span><br><span class="line">	System.out.println(b);  <span class="comment">//输出：false</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">*  String substring(int beginIndex,int endIndex) 获取字符串的一部分</span></span><br><span class="line"><span class="comment">*  返回新的字符串</span></span><br><span class="line"><span class="comment">*  包含头,不包含尾巴</span></span><br><span class="line"><span class="comment">*  </span></span><br><span class="line"><span class="comment">*  String substring(int beginIndex)获取字符串的一部分</span></span><br><span class="line"><span class="comment">*  包含头,后面的字符全要</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">StringFunction</span><span class="params">()</span></span>&#123;</span><br><span class="line">	String str = <span class="string">"haijd"</span>;</span><br><span class="line">	<span class="comment">//调用String类方法substring获取字符串一部分</span></span><br><span class="line">	str= str.substring(<span class="number">1</span>, <span class="number">3</span>);</span><br><span class="line">	System.out.println(str);   <span class="comment">//输出：ai</span></span><br><span class="line">	String str2 = <span class="string">"haijd"</span>;</span><br><span class="line">	str2 = str2.substring(<span class="number">2</span>);</span><br><span class="line">	System.out.println(str2);  <span class="comment">//输出：ijd</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">*  int length() 返回字符串的长度</span></span><br><span class="line"><span class="comment">*  包含多少个字符</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">StringFunction</span><span class="params">()</span></span>&#123;</span><br><span class="line">	String str = <span class="string">"haijd"</span>;</span><br><span class="line">	<span class="comment">//调用String类方法length,获取字符串长度</span></span><br><span class="line">	<span class="keyword">int</span> length = str.length();</span><br><span class="line">	System.out.println(length);  <span class="comment">//输出：5</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="entry-meta entry-footer">
      
	<span class="ico-folder"></span>
    <a class="article-category-link" href="/categories/Java/">Java</a>

      
  <span class="ico-tags"></span>
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Java基础/">Java基础</a></li></ul>

      
        <div id="donation_div"></div>

<script src="/js/vdonate.js"></script>
<script>
var a = new Donate({
  title: '如果觉得我的文章对您有用，请随意打赏。您的支持将鼓励我继续创作!', // 可选参数，打赏标题
  btnText: 'Donate', // 可选参数，打赏按钮文字
  el: document.getElementById('donation_div'),
  wechatImage: '/css/images/wechatPay.png',
  alipayImage: '/css/images/alipay.jpg'
});
</script>
      

      
        
	<section id="comments" class="comment">
	  <div id="disqus_thread">
	  <noscript>Please enable JavaScript to view the <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
	  </div>
	</section>

	<script type="text/javascript">
	var disqus_shortname = 'haijdblog';
	(function(){
	  var dsq = document.createElement('script');
	  dsq.type = 'text/javascript';
	  dsq.async = true;
	  dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
	  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	}());
	(function(){
	  var dsq = document.createElement('script');
	  dsq.type = 'text/javascript';
	  dsq.async = true;
	  dsq.src = '//' + disqus_shortname + '.disqus.com/count.js';
	  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	}());
	</script>


      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2017/05/22/Java基础：正则表达式/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Java基础：正则表达式
        
      </div>
    </a>
  
  
    <a href="/2017/05/21/Java基础：StringBuffer-StringBuilder/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Java基础：StringBuffer&amp;StringBuilder</div>
    </a>
  
</nav>

  
</article>

<!-- Table of Contents -->

  <aside id="sidebar">
    <div id="toc" class="toc-article">
    <strong class="toc-title">Contents</strong>
    
      <ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#String类的概念和不变性"><span class="nav-number">1.</span> <span class="nav-text">String类的概念和不变性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#String类创建方式和比较"><span class="nav-number">2.</span> <span class="nav-text">String类创建方式和比较</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#String类构造方法"><span class="nav-number">3.</span> <span class="nav-text">String类构造方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#String类的其他方法"><span class="nav-number">4.</span> <span class="nav-text">String类的其他方法</span></a></li></ol>
    
    </div>
  </aside>
</section>
        
      </div>
      <footer id="footer" class="site-footer">
  

  <div class="clearfix container">
      <div class="site-info">
	      &copy; 2018 haijd All Rights Reserved.
          
            <span id="busuanzi_container_site_uv">
              本站访客数<span id="busuanzi_value_site_uv"></span>人次  
              本站总访问量<span id="busuanzi_value_site_pv"></span>次
            </span>
          
      </div>
      <div class="site-credit">
        Theme by <a href="https://github.com/iTimeTraveler/hexo-theme-hiero" target="_blank">hiero</a>
      </div>
  </div>
</footer>


<!-- min height -->

<script>
    var contentdiv = document.getElementById("content");

    contentdiv.style.minHeight = document.body.offsetHeight - document.getElementById("allheader").offsetHeight - document.getElementById("footer").offsetHeight + "px";
</script>
    </div>
    <!-- <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
    <a href="/categories" class="mobile-nav-link">Categories</a>
  
    <a href="/tags" class="mobile-nav-link">Tags</a>
  
    <a href="/about" class="mobile-nav-link">About</a>
  
</nav> -->
    

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/scripts.js"></script>
<script src="/js/bootstrap.js"></script>
<script src="/js/main.js"></script>








	<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
	</script>






  </div>

  <a id="rocket" href="#top" class=""></a>
  <script type="text/javascript" src="/js/totop.js" async=""></script>
</body>
</html>
